﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA_TD.Menue
{
    public class UnitHealth : IRunable, IDraw
    {
        public UnitHealth()
            : base(Priority.Effekt)
        {
        }

        public void Draw()
        {
            int heightDiff = graphics.GraphicsDevice.Viewport.Height / 20;
            var depthStencilState = graphics.GraphicsDevice.DepthStencilState;
            spriteBatch.Begin();

            foreach (var render in gameManager.getRenderList(Priority.Einheit))
            {
                var unit = render as Unit;
                if (unit != null)
                {
                    DrawHealth(unit, .5f);
                }
            }
            DrawHealth(baseTower, 1.5f);

            spriteBatch.End();
            graphics.GraphicsDevice.DepthStencilState = depthStencilState;
        }

        private void DrawHealth(GameObject gameObject, float heightDiff)
        {
            Vector3 center = graphics.GraphicsDevice.Viewport.Project(gameObject.position + new Vector3(0,heightDiff,0),
                projectionMatrix, viewMatrix, Matrix.Identity);

            string txt = (int)gameObject.health + "/" + (int)gameObject.lvlInfo.maxHealth;
            Vector2 pos = new Vector2(center.X, center.Y) - DebugInfo.Instance.debugFont.MeasureString(txt) * .5f;
            spriteBatch.DrawString(DebugInfo.Instance.debugFont, txt , pos, Color.White);
        }

        public override void Load()
        {
        }

        public override void UnLoad()
        {
        }
    }
}
